package com.have.blog.admin.controller;

import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.BlogDto;
import com.have.blog.admin.service.AdminBlogService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Delete;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// todo
@RestController
@RequestMapping("/blog")
@Api(value = "博客相关接口", tags = {"博客相关接口"})
@Slf4j
public class AdminBlogController {
    @Autowired
    private AdminBlogService blogService;

    @ApiOperation(value = "获取博客列表", notes = "获取博客列表")
    @PostMapping("/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody BlogDto blogDto, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        List<BlogDto> list = blogService.getList(blogDto);
        return Wrapper.success(CommonPage.restPage(list));
    }

    @OperationLogger(value = "添加博客")
    @ApiOperation(value = "添加博客", notes = "添加博客")
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody BlogDto blogDto, BindingResult result) {
        System.out.println("添加博客");
        ThrowableUtils.checkParamArgument(result);
        return blogService.add(blogDto);
    }

    @OperationLogger(value = "编辑博客")
    @ApiOperation(value = "修改博客", notes = "修改博客")
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody BlogDto blogDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return blogService.edit(blogDto);
    }


    @ApiOperation(value = "修改博客", notes = "修改博客")
    @PostMapping("/delete")
    public Wrapper delete(@Validated({Delete.class}) @RequestBody BlogDto blogDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return blogService.delete(blogDto);
    }


    @OperationLogger(value = "删除选中博客")
    @ApiOperation(value = "删除选中博客", notes = "删除选中博客")
    @PostMapping("/deleteBatch")
    public Wrapper deleteBatch(@RequestBody List<Integer> uids) {
        return blogService.deleteBatchBlog(uids);
    }

}
